<?php
namespace app\controller;

use app\BaseController;
use think\facade\Request;
use think\facade\Db;
use think\Exception;
use think\facade\Filesystem;
use think\facade\Log;
use think\Event;
use app\event\AssetHistoriesEvent;

class LabelTemplate extends BaseController
{
    //获取全部标签模板
    public function getAllLabelTemplate()
    {
        $userRoot = $this->request->auth['data']->user->user_root;
        $userId = $this->request->auth['data']->user->user_id;


        // 根据用户ID获取公司ID
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');

        //直接获取该公司下的所有标签模板
        $labelTemplates = Db::table('vw_LabelTemplate')
            ->where('LabelTemplate_company_id', $companyId)
            ->select();

        //以json格式返回前端
        return json(['code'=>200, 'msg'=>'获取标签模板成功', 'data'=>$labelTemplates]);

    }

    //新增标签模板
    public function add_LabelTemplate()
    {
        $userRoot = $this->request->auth['data']->user->user_root;
        $userId = $this->request->auth['data']->user->user_id;
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');

        $data=Request::param('data');


        try {
            //开启事务
            Db::startTrans();
            //判断data中是否有LabelTemplate_Templatejson字段数据如果没有则返回错误
            if (!isset($data['LabelTemplate_Templatejson'])) {
                return json(['code'=>400, 'msg'=>'请传入标签模板数据']);
            }

            if (!isset($data['LabelTemplate_name'])) {
                return json(['code'=>400, 'msg'=>'请传入标签模板名称']);
            }
            //构造数据集
            $adddata = [
                'LabelTemplate_name'=>$data['LabelTemplate_name'],
                'LabelTemplate_Templatejson'=>json_encode($data['LabelTemplate_Templatejson']),
                'LabelTemplate_company_id'=>$companyId,
                'LabelTemplate_user_id'=>$userId
            ];
           
            //直接一次性插入数据集data
            $result = Db::table('LabelTemplate')->insert($adddata);
            //关闭事务
            Db::commit();


            //返回结果
            if ($result) {
                return json(['code'=>200, 'msg'=>'新增标签模板成功']);
            }
            else {
                return json(['code'=>400, 'msg'=>'新增标签模板失败']);
            }

        }
        catch (Exception $e) {
            //回滚事务
            Db::rollback();
            return json(['code'=>400, 'msg'=>$e->getMessage()]);
        }

    }

    //更新标签模板
    public function update_LabelTemplate()
    {
        $userRoot = $this->request->auth['data']->user->user_root;
        $userId = $this->request->auth['data']->user->user_id;
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');
        try {
            $LabelTemplate_id=Request::param('LabelTemplate_id');
            $data=Request::param('data');
            if (!isset($LabelTemplate_id)) {
                return json(['code'=>400, 'msg'=>'请传入标签模板ID']);
            }
            //判断是否有LabelTemplate_Templatejson字段数据
            if (!isset($data['LabelTemplate_Templatejson'])) {
                return json(['code'=>400, 'msg'=>'请传入标签模板数据']);
            }
            //将模版字段json_encode处理
            $data['LabelTemplate_Templatejson'] = json_encode($data['LabelTemplate_Templatejson']);
            $result = Db::table('LabelTemplate')->where('LabelTemplate_id', $LabelTemplate_id)->update($data);
            if ($result) {
                return json(['code'=>200, 'msg'=>'更新标签模板成功']);
            }
            else {
                return json(['code'=>400, 'msg'=>'更新标签模板失败']);
            }
        }
        catch (Exception $e) {
            return json(['code'=>400, 'msg'=>$e->getMessage()]);
        }
    }

    //删除标签模板
    public function delete_LabelTemplate()
    {
        $userRoot = $this->request->auth['data']->user->user_root;
        $userId = $this->request->auth['data']->user->user_id;
        try {
            $LabelTemplate_id=Request::param('LabelTemplate_id');
            if (!isset($LabelTemplate_id)) {
                return json(['code'=>400, 'msg'=>'请传入标签模板ID']);
            }
            $result = Db::table('LabelTemplate')->where('LabelTemplate_id', $LabelTemplate_id)->delete();
            if ($result) {
                return json(['code'=>200, 'msg'=>'删除标签模板成功']);
            }
            else {
                return json(['code'=>400, 'msg'=>'删除标签模板失败']);
            }
        }
        catch (Exception $e) {
            return json(['code'=>400, 'msg'=>$e->getMessage()]);
        }
    }
    //将模版设为默认模版
    public function setDefault_LabelTemplate()
    {
        $userRoot = $this->request->auth['data']->user->user_root;
        $userId = $this->request->auth['data']->user->user_id;
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');
        try {
            //开启事务
            Db::startTrans();
            $LabelTemplate_id=Request::param('LabelTemplate_id');
            if (!isset($LabelTemplate_id)) {
                return json(['code'=>400, 'msg'=>'请传入标签模板ID']);
            }
            //将模版LabelTemplate_isdefault字段设为1,并将其他所有的模版设为0
            $result = Db::table('LabelTemplate')->where('LabelTemplate_company_id', $companyId)->update(['LabelTemplate_isdefault'=>0]);
            if ($result) {
                $result = Db::table('LabelTemplate')->where('LabelTemplate_id', $LabelTemplate_id)->update(['LabelTemplate_isdefault'=>1]);
                if ($result) {
                    //关闭事务
                    Db::commit();
                    return json(['code'=>200, 'msg'=>'设置默认模版成功']);
                }
                else {
                    //回滚事务
                    Db::rollback();
                    return json(['code'=>400, 'msg'=>'设置默认模版失败']);
                }
            }

        }
        catch (Exception $e) {
            //回滚事务
            Db::rollback();
            return json(['code'=>400, 'msg'=>$e->getMessage()]);
        }
    }
    //获取默认模版
    public function getDefault_LabelTemplate()
    {
        $userRoot = $this->request->auth['data']->user->user_root;
        $userId = $this->request->auth['data']->user->user_id;
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');
        try {
            $result = Db::table('LabelTemplate')->where('LabelTemplate_company_id', $companyId)->where('LabelTemplate_isdefault', 1)->find();
            //判断是否为空，如果为空则返回为空的错误代码
            if (empty($result)) {
                return json(['code'=>400, 'msg'=>'请新建模版或者设置默认模版']);
            }
            if ($result) {
                return json(['code'=>200, 'msg'=>'获取默认模版成功', 'data'=>$result]);
            }
            else {
                return json(['code'=>400, 'msg'=>'获取默认模版失败']);
            }
        }
        catch (Exception $e) {
            return json(['code'=>400, 'msg'=>$e->getMessage()]);
        }
    }

    //根据传入的资产id数组获取对应的资产并且构造对应的二维码数据
    public function get_AssetsLabel()
    {
        $userRoot = $this->request->auth['data']->user->user_root;
        $userId = $this->request->auth['data']->user->user_id;
        $companyId = Db::name('user')->where('user_id', $userId)->value('user_company_id');

        try {
            $assetsId = Request::param('assetsId');
            //判断是否为空，以及判断是否为数组
            if (empty($assetsId) || !is_array($assetsId)) {
                return json(['code'=>400, 'msg'=>'请传入资产id数组']);
            }
            //根据资产id数组获取对应的资产数据
            $result = Db::table('vw_assets')->where('assets_company_id', $companyId)->whereIn('assets_id', $assetsId)->select();
            // 创建一个新的数组来存储带有二维码URL的数据
             $newResult = [];
            // 遍历原始结果并为每一条记录添加一个二维码 URL
            foreach ($result as $item) {
                $newItem = $item; // 复制原始项
                $newItem['assets_URcode'] = 'https://asset.zichan.xyz/assetURcode?asset_id=' . $item['assets_id'].'&asset_code=' . $item['assets_code'];
                $newResult[] = $newItem; // 将带有二维码URL的新项添加到新数组中
            }

            if ($result) {
                return json(['code'=>200, 'msg'=>'获取资产成功', 'data'=>$newResult]);
            }
            else {
                return json(['code'=>400, 'msg'=>'获取资产失败']);
            }
        }
        catch (Exception $e) {
            return json(['code'=>400, 'msg'=>$e->getMessage()]);
        }

    }

}